package com.he.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.he.pojo.ServiceResult;
import com.he.pojo.User;
import com.he.services.impl.UserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: usersync
 * @description:
 * @author: xin.he
 * @create: 2021-06-06 12:20
 **/

@RestController
@RequestMapping("/user")
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserServiceImpl userService;

    @GetMapping("/queryAll")
    public List<User> queryAll (){
        return userService.queryAll();
    }

    @GetMapping("/syncUser")
    public ServiceResult<String> syncDept(){

        try {
            String msg = userService.syncUser();
            log.info(msg);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ServiceResult.failure("400",e.getMessage());
        }
        return ServiceResult.success("正在同步,请稍后...");
    }

    /**
     *
     * 返回List的分页
     * @param pageNum 页码
     * @param pageSize 每页显示数量
     * @return
     */
    @GetMapping("/pageUserList")
    public ServiceResult<List<User>> pageUserList (int pageNum, int pageSize) {

        return ServiceResult.success(userService.pageUserList(pageNum, pageSize));
    }

    /**
     * 返回分页对象的分页
     * @param pn pageNum页码
     * @return
     */
    @GetMapping("/page/{pn}/{size}")
    public ServiceResult<PageInfo> userPage(@PathVariable Integer pn, @PathVariable Integer size){
            PageHelper.startPage(pn, size);
            List<User>  userList = userService.queryAll();
            //navigatePages 页码数量
            PageInfo pageInfo = new PageInfo(userList , 8);
            PageHelper.clearPage();

        return ServiceResult.success(pageInfo);
    }


}
